package edu.lfsy.rabbitmqdemo.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 王希辰
 * @date 2022/4/17 0:02
 */
@Service
@Slf4j
public class rabbitMQService {
    /*@Autowired
    private AmqpAdmin amqpAdmin;
    @Autowired(required = false)
    public void setAmqpAdmin(AmqpAdmin amqpAdmin){
        //定义fanout类型的交换器
        amqpAdmin.declareExchange(new FanoutExchange("fanout_exchange"));
        //定义两个消息对列，分别处理email和sms
        amqpAdmin.declareBinding(new Binding("fanout_exchange_email",Binding.DestinationType.QUEUE,"fanout_exchange","",null));
        amqpAdmin.declareBinding(new Binding("fanout_exchange_sms",Binding.DestinationType.QUEUE,"fanout_exchange","",null));
        this.amqpAdmin=amqpAdmin;
    }*/


    //Publish/Subscribe工作模式接收、处理邮件业务
//    @RabbitListener(queues = "fanout_exchange_email")
//    @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_exchange_email"),
//            exchange = @Exchange(value = "fanoutExchange",type = "fanout")))
//    public void psubConsumerEmail1(Message message){
//        byte[] body=message.getBody();
//        String s=new String(body);
//        log.info("接收fanout_exchange_email队列消息："+s);
//    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_exchange_email"),
            exchange = @Exchange(value = "fanout_exchange",type = "fanout")))
    public void psubConsumerEmail(Message message){
        byte[] body=message.getBody();
        String s=new String(body);
        log.info("接收fanout_exchange_email队列消息："+s);
    }
    //Publish/Subscribe工作模式接收、处理短信业务
    //@RabbitListener(queues = "fanout_exchange_sms")
    @RabbitListener(bindings = @QueueBinding(value = @Queue("fanout_exchange_sms"),
            exchange = @Exchange(value = "fanout_exchange",type = "fanout")))
    public void psubConsumerSms(Message message){
        byte[] body=message.getBody();
        String s=new String(body);
        log.info("接收fanout_exchange_email队列消息："+s);
    }
    //路由模式消息接收、处理error级别的日志信息
    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("routing_queue_error"),exchange =
    @Exchange(value ="routing_exchange",type = "direct"),
    key = "error_routing_key"))
    public void routingConsumerError(String message){
        log.info("接收到error级别的日志信息："+message);
    }
    //路由模式消息接收、处理error级别的日志信息
    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("routing_queue_info_warning"),exchange =
    @Exchange(value ="routing_exchange",type = "direct"),
            key = "info_warning_routing_key"))
    public void routingConsumerInfoAndWarning(String message){
        log.info("接收到error级别的日志信息："+message);
    }
    //通配符模式消息接收、进行邮件业务订阅处理
    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("topic_queue_email"),exchange =
    @Exchange(value ="topic_exchange",type = "topic"),
            key = "info.#.email.#"))
    public void topicConsumerEmail(String message){
        log.info("通配符接收到邮件信息："+message);
    }
    //通配符模式消息接收、进行短信业务订阅处理
    @RabbitListener(bindings =@QueueBinding(value =
    @Queue("topic_queue_sms"),exchange =
    @Exchange(value ="topic_exchange",type = "topic"),
            key = "info.#.sms.#"))
    public void topicConsumerSms(String message){
        log.info("通配符接收到短信信息："+message);
    }



























}
